﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.IO;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;

namespace FormsAppT8
{
    public partial class Form1 : Form
    {
        dbconn db = new dbconn();
        string ID;
        string BD;
        double count = 0;
        int bsh = 0;
        public Form1(int flag,string billdate,string billno,string kh)
        {
            InitializeComponent();
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            label1.Text = "null";

            label29.Text = billdate.Substring(0, 4) + "-" + billdate.Substring(4, 2) + "-" + billdate.Substring(6, 2);
            BD = billdate;
            label30.Text=billno;
            label31.Text=kh;
            label34.Text = flag.ToString();
            if (flag == 1000)
            { ID = "1"; }
            else if (flag == 1002)
            { ID = "0"; }
            mx();
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        
        }
        public void mx()
        {
            string sql = "select a.RowNo,a.CU_KHCGDDH,b.MaterialId,b.MaterialName,b.MaterialSpec,a.SQuantity,a.Remark,a.CU_count,a.BPMaterialId from salDispatchListDetail a,comMaterialGroup b where a.SupplyOrgId='" + label34.Text + "' and a.billno='" + label30.Text + "' and a.MaterialId=b.MaterialId";
            //string sql = "select a.RowNo,'',b.MaterialId,b.MaterialName,b.MaterialSpec,a.SQuantity,a.Remark,'0',a.BPMaterialId from salDispatchListDetail a,comMaterialGroup b where a.SupplyOrgId='" + flag + "' and a.billno='" + billno + "' and a.MaterialId=b.MaterialId";
            dataGridView1.Rows.Clear();
            db.databind(sql, "salDispatchListDetail", dataGridView1);
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                dataGridView1.Rows[i].Cells[5].Value = Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value.ToString());
                dataGridView1.Rows[i].Cells[7].Value = Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value.ToString());
                //dataGridView1.Rows[i].Cells[7].Value = "0";
            }
        }
        public void ewmsc() 
        {
            if (textBox1.Text == "")
            {
                label1.Text = "null";
                //MessageBox.Show("Data must not be empty.");
                return;
            }
            else
            {
                label1.Text = textBox1.Text + "\r\n"
                   + "Order Number: " + textBox2.Text + "\r\n"
                   + "Part No: " + textBox3.Text + "\r\n"
                   + "Desc: " + textBox4.Text + "\r\n"
                   + "Lot Code: " + textBox5.Text + "\r\n"
                   + "Manufacturing Date: " + textBox6.Text + "\r\n"
                   + "Quantity: " + textBox7.Text + "\r\n"
                   + "MPN:          NCMR:" + "\r\n"
                   + "Vendor Name:NingBoDianGong" + "\r\n"
                   + "        宁波电工";
            }
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale =2;
            qrCodeEncoder.QRCodeVersion =10;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
          
            Image image;
            string data = textBox1.Text + "&" + textBox2.Text + "&" + textBox3.Text + "&" + textBox4.Text + "&" + textBox5.Text + "&" + textBox6.Text + "&" + textBox7.Text;
            image = qrCodeEncoder.Encode(data, Encoding.UTF8);

            pictureBox1.Image = image;
        }

        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            click();
         
        }
        public void click()
        {
            string num = "";
            count = 0;
            for (int j = 0; j < dataGridView1.RowCount; j++)
            {
                count = count + Convert.ToDouble(dataGridView1.Rows[j].Cells[7].Value.ToString());
            }
            if ((count + 1) < 10)
            {
                num = "000" + (count + 1).ToString();
            }
            else if ((count + 1) < 100)
            {
                num = "00" + (count + 1).ToString();
            }
            else if ((count + 1) < 1000)
            {
                num = "0" + (count + 1).ToString();
            }
            else
            {
                num = (count + 1).ToString();
            }

            string xx = "";
            if (dataGridView1.SelectedRows[0].Cells[2].Value.ToString().Substring(0, 2) == "BP")
            {
                xx = "1359";
            }
            else
            {
                xx = "1389";
            }

            label33.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string x1 = "STRIXV00" + xx + BD + ID + num;
            //MessageBox.Show(xx+"  "+BD+"  "+ID+"  "+num);
            string x2;
            if (dataGridView1.SelectedRows[0].Cells[1].Value == null)
            {
                x2 = "";
            }
            else
            {
                x2 = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            }
            string x3;
            if (dataGridView1.SelectedRows[0].Cells[8].Value == null)
            {
                x3 = "";
            }
            else
            {
                x3 = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
            }
            string x4;
            if (dataGridView1.SelectedRows[0].Cells[4].Value == null)
            {
                x4 = "";
            }
            else
            {
                x4 = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            }
            string x5;
            if (dataGridView1.SelectedRows[0].Cells[6].Value == null)
            {
                x5 = "";
            }
            else
            {
                x5 = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
            }
            string x6 = label29.Text;
            string x7 = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
            textBox1.Text = x1;
            //label1.Text = x1;
            label25.Text = x1;
            textBox2.Text = x2;
            //label18.Text = x2;
            textBox3.Text = x3;
           // label19.Text = x3;
            textBox4.Text = x4;
            //label20.Text = x4;
            textBox5.Text = x5;
           // label21.Text = x5;
            textBox6.Text = x6;
            //label22.Text = x6;
            textBox7.Text = x7;
           // label23.Text = x7 + " PCS";
            ewmsc();
        }
        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {
            label1.Text = textBox1.Text;
            label25.Text = textBox1.Text;
            ewmsc();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
          
            ewmsc();
        }

        private void textBox3_TextChanged_1(object sender, EventArgs e)
        {
           
            ewmsc();
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
           
            ewmsc();
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            
            ewmsc();
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
           
            ewmsc();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            dy();
        }

        public void dy()
        {
            if (comboBox1.Text == "") { MessageBox.Show("请选择打印份数", "提示信息"); }
            else if (Convert.ToInt16(comboBox1.Text) > 0)
            {
                for (int j = 0; j < Convert.ToInt16(comboBox1.Text); j++)
                {

                    //count=0;
                    for (int i = 0; i < dataGridView1.RowCount; i++)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value.ToString() == label33.Text)
                        {

                            this.dataGridView1.Rows[i].Selected = true;
                        }
                    }
                    string num = "";
                    if ((count + 1) < 10)
                    {
                        num = "000" + (count + 1).ToString();
                    }
                    else if ((count + 1) < 100)
                    {
                        num = "00" + (count + 1).ToString();
                    }
                    else if ((count + 1) < 1000)
                    {
                        num = "0" + (count + 1).ToString();
                    }
                    else
                    {
                        num = (count + 1).ToString();
                    }
                    string xx = "";
                    if (dataGridView1.SelectedRows[0].Cells[2].Value.ToString().Substring(0, 2) == "BP")
                    {
                        xx = "1359";
                    }
                    else
                    {
                        xx = "1389";
                    }

                    label33.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                    string x1 = "STRIXV00" + xx + BD + ID + num;
                    textBox1.Text = x1;
                    count = count + 1;
                    ewmsc();

                    //printDialog1.Document = printDocument1;
                    //if (printDialog1.ShowDialog() == DialogResult.OK)
                    //{
                        //设置打印时横向还是纵向
                        //printDocument1.PrinterSettings.Copies = Convert.ToInt16(comboBox1.Text);
                        printDocument1.PrinterSettings.Copies = 1;
                        printDocument1.DefaultPageSettings.Landscape = false;
                        printDocument1.DefaultPageSettings.Margins = new Margins(20, 20, 20, 0);
                        PrintDocument prtdoc = new PrintDocument();
                        printDocument1.PrinterSettings.PrinterName = prtdoc.PrinterSettings.PrinterName;
                        printDocument1.Print();
                        string sql = "update salDispatchListDetail set cu_count=cu_count+1 where billno='" + label30.Text + "' and rowno='" + label33.Text + "'";
                        db.sqlcommand(sql);
                        mx();
                        dataGridView1.ClearSelection();
                        
                       
                   // }
                }
            }
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value.ToString() == label33.Text)
                {

                    this.dataGridView1.Rows[i].Selected = true;
                }
            }
            string x4 = "";
            if ((count + 1) < 10)
            {
                x4 = "000" + (count + 1).ToString();
            }
            else if ((count + 1) < 100)
            {
                x4 = "00" + (count + 1).ToString();
            }
            else if ((count + 1) < 1000)
            {
                x4 = "0" + (count + 1).ToString();
            }
            else
            {
                x4 = (count + 1).ToString();
            }
            string x2 = "";
            if (dataGridView1.SelectedRows[0].Cells[2].Value.ToString().Substring(0, 2) == "BP")
            {
                x2 = "1359";
            }
            else
            {
                x2 = "1389";
            }

            label33.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string x3 = "STRIXV00" + x2 + BD + ID + x4;
            textBox1.Text = x3;
            count = count + 1;
            ewmsc();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
            panel1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
